package com.wxzz.elearing.course.api.admin;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.dto.StudyPlanInsDto;
import com.wxzz.elearing.course.entity.TrainPlan;
import com.wxzz.elearing.course.service.ITrainPlanService;
import com.wxzz.elearing.course.vo.TrainPlanVo;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/train-plan")
@Api(tags = "培训计划(后台)")
public class TrainPlanController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private ITrainPlanService iTrainPlanService;

    @ApiOperation(value = "添加培训计划(后台)", notes = "添加培训计划(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveTrainPlan", method = RequestMethod.POST)
    public ReturnVO saveStudyPlan(@RequestHeader String token, @RequestBody TrainPlan trainPlan) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        trainPlan.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        trainPlan.setCreateId(jsonObject.getLong("id"));
        return iTrainPlanService.saveTrainPlan(trainPlan);
    }

    @ApiOperation(value = "培训计划列表(后台)", notes = "培训计划列表(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "trainPlanList", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<TrainPlan>>> trainPlanList(@RequestBody TrainPlanVo trainPlan, @RequestHeader String token) throws IOException {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        trainPlan.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        return iTrainPlanService.selectTrainPlanList(trainPlan);
    }


    @ApiOperation(value = "删除培训计划(后台)", notes = "删除培训计划(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteTrainPlan", method = RequestMethod.POST)
    public ReturnVO deleteTrainPlan(@RequestHeader String token, @RequestBody TrainPlan trainPlan) {
        return iTrainPlanService.deleteTrainPlan(trainPlan);
    }

    @ApiOperation(value = "培训计划详情(后台)", notes = "培训计划详情(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "trainPlanById/{id}", method = RequestMethod.POST)
    public ReturnVO trainPlanById(@PathVariable(value = "id") Long id) {
        return iTrainPlanService.trainPlanById(id);
    }
}
